package view;

import model.GameManager;
import model.GameModelInterface;
import touche.touche.EndGameActivity;
import touche.touche.GameActivity;
import touche.touche.PauseActivity;
import touche.touche.R;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;
import controller.EndGameObserver;
import controller.GameControllerInterface;
import controller.GridPicturesObserver;
import controller.InfoBarObserver;
import controller.LevelNameObserver;
import controller.PauseObserver;

public class GameView implements InfoBarObserver, LevelNameObserver,
		GridPicturesObserver, PauseObserver, EndGameObserver {
	private GameModelInterface _gameModel;
	private GameControllerInterface _gameController;

	private GameActivity _gameActivity;

	private TextView _textViewLife = null;
	private ProgressBar _progressBarLife = null;
	private TextView _textViewScore = null;
	private TextView _textViewTime = null;
	private TextView _textViewLevel = null;
	private Button _buttonPause = null;
	private GridView _gridViewPictures = null;

	private MediaPlayer _player = null;

	public GameView(GameModelInterface model, GameControllerInterface controller) {
		// Initializations
		this._gameModel = model;
		this._gameController = controller;

		// We register observers to be notified by the model when it has changes
		_gameModel.registerObserver((InfoBarObserver) this);
		_gameModel.registerObserver((LevelNameObserver) this);
		_gameModel.registerObserver((GridPicturesObserver) this);
		_gameModel.registerObserver((EndGameObserver) this);
		_gameModel.registerObserver((PauseObserver) this);
	}

	public GameModelInterface getModel() {
		return _gameModel;
	}

	public void setActivity(GameActivity activity) {
		this._gameActivity = activity;
	}

	public GameControllerInterface getController() {
		return _gameController;
	}

	public void setListeners() {
		_buttonPause.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				_gameController.pause();
			}
		});

		_gridViewPictures.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				playSound(R.raw.touch);
				_gameController.touch(position);
			}
		});

		_gridViewPictures.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_MOVE) {
					return true;
				}
				return false;
			}
		});
	}

	private void playSound(int sound) {
		Log.e("Gameview", "playSound sound=" + sound);
		if (_player != null) {
			_player.stop();
			_player.release();
		}
		_player = MediaPlayer.create(_gameActivity, sound);
		_player.start();
	}

	public void setWidgets(TextView textViewLife, ProgressBar progressBarLife,
			TextView textViewScore, TextView textViewTime,
			TextView textViewLevel, Button buttonPause,
			GridView gridViewPictures) {
		this._textViewLife = textViewLife;
		this._progressBarLife = progressBarLife;
		this._textViewScore = textViewScore;
		this._textViewTime = textViewTime;
		this._textViewLevel = textViewLevel;
		this._buttonPause = buttonPause;
		this._gridViewPictures = gridViewPictures;
		_gameModel.update();
	}

	public void updateGridPictures(Integer[] pictures, Integer[] visibles) {
		_gridViewPictures.setAdapter(new LevelImageAdapter(_gridViewPictures
				.getContext(), pictures, visibles));
	}

	public void updateInfoBar(int life, int score, int time) {
		this._textViewLife.setText(new String(String.valueOf(life) + "%"));
		this._progressBarLife.setProgress(life);
		this._textViewScore.setText(new String(String.valueOf(score)));
		this._textViewTime.setText(new String(String.valueOf(time)));
	}

	public void updateLevelName(String name) {
		this._textViewLevel.setText(name);
	}

	public void updateToPause(String levelName, int score, int time, int life) {
		Intent intent = new Intent(_gameActivity, PauseActivity.class);
		intent.putExtra("levelName", levelName);
		intent.putExtra("score", score);
		intent.putExtra("time", time);
		intent.putExtra("life", life);
		_gameActivity.startActivity(intent);
	}

	public void updateToEndGame(String levelName, int score) {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Log.e("gameview", "endGame");
		Intent intent = new Intent(_gameActivity, EndGameActivity.class);
		intent.putExtra("levelName", _gameModel.getLevel().getName());
		intent.putExtra("score", _gameModel.getScore());
		_gameActivity.startActivity(intent);
		GameManager.getInstance().init();
	}

}
